package com.lyrcsoft.controller;

import com.lyrcsoft.common.dto.OrderDTO;
import com.lyrcsoft.common.exception.RRException;
import com.lyrcsoft.common.utils.R;
import com.lyrcsoft.entity.OrderEntity;
import com.lyrcsoft.service.OrderService;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 功能描述
 *
 * @author fujc-dev@qq.com
 * @date: 2023年08月24日 14:47
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {


    @Resource
    private OrderService orderService;


    @RequestMapping("/findOrderByUserId/{user_id}")
    public R findOrderByUserId(@PathVariable("user_id") Integer userId) {
        // 模拟参数异常
        if (userId == 5) {
            throw new RRException("非法参数异常");
        }
        if (userId == 6) {
            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        log.info("根据userId：" + userId + "查询订单信息。");
        List<OrderEntity> orderEntities = orderService.listByUserId(userId);
        return R.ok().put("orders", orderEntities);
    }


    @RequestMapping(value = "/orderInfo")
    public R findOrder(@RequestParam Integer userId, @RequestParam String commodityCode) {

        List<OrderEntity> orderEntities = orderService.listByUserIdAndCommodityCode(userId, commodityCode);
        return R.ok().put("orders", orderEntities);
    }

    @RequestMapping(value = "/findOrderByUserIdAndCode")
    public R findOrderByUserIdAndCode(@RequestBody OrderDTO orderDTO) {
        List<OrderEntity> orderEntities = orderService.listByUserIdAndCommodityCode(orderDTO.getUserId(), orderDTO.getCommodityCode());
        return R.ok().put("orders", orderEntities);
    }
}
